package com.forestar.mapControl.utils.mapConvert.task;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Environment;

import com.forestar.mapControl.utils.BitmapUtils;
import com.forestar.mapControl.utils.FileSizeUtil;
import com.forestar.mapControl.utils.LogD;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * created by  xzk
 * 2020/8/4   14:20
 * 说明:  获取目标数量的文件当图片
 */
public class LoadFileTileTask extends LoadTileCommonTask {

    public LoadFileTileTask(AtomicInteger hasTaskNum) {
        super(hasTaskNum);
        setMaxNumber();
    }

    private void setMaxNumber(){
        this.fileMaxSize = 1000;
    }

    @Override
    public void getBitmapListFromFile() {
        //外层testGis文件夹
        String tempFileDirectory = buildTempFileDirectory();
        File t1 = new File(tempFileDirectory);
        File[] files = t1.listFiles();
        if(null == files || files.length<1){
            return;
        }
        //保存了多个图层的文件夹
        for(File file:files){
            if(!file.isDirectory()){
            }else {
                if(!file.getPath().contains("no")) {
                    File[] tileList = file.listFiles();
                    //拿到地图文件
                    if(null != tileList && tileList.length>0) {
                        for(File tile:tileList) {
                            if(!tile.isDirectory()){
                                //通过大小判定，筛选颜色鲜艳的瓦片
                                double orFilesSize = FileSizeUtil.getFileOrFilesSize(tile.getAbsolutePath(), FileSizeUtil.SIZETYPE_KB);
                                if(orFilesSize>15){
                                    Bitmap bitmap = BitmapFactory.decodeFile(tile.getAbsolutePath());
                                    bitmap = BitmapUtils.drawBg4Bitmap(Color.WHITE, bitmap);
                                    bitmapTempList.add(bitmap);
                                }
                                if(bitmapTempList.size()>fileMaxSize-1){
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        //不符合最大数量，再次添加  最大为1000
        int index = 0;
        while (bitmapTempList.size()<fileMaxSize){
            bitmapTempList.add(0,bitmapTempList.get(index));
            index=index+3;
            if(index>100){
                index = 0;
            }
        }
    }
}
